//
//  InputFontViewController.m
//  MJRefresh
//
//  Created by mac on 2018/8/21.
//  Copyright © 2018年 YangYang. All rights reserved.
//

#import "InputFontViewController.h"

@interface InputFontViewController ()

@property (nonatomic, strong) NSString *str1;
@property (nonatomic, strong) UILabel *titleLableBig;
//@property (nonatomic, strong) NSThread *thred;

@end

@implementation InputFontViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createUI];
}

-(void)createUI
{
    UILabel *animationView = [[UILabel alloc] init];
    animationView.numberOfLines = 0;
    [self.view addSubview:animationView];
    [animationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX).mas_offset(0);
        make.top.mas_equalTo(self.view.mas_top).mas_offset(64 + 20);
        make.width.mas_equalTo(ScreenWidth - 20);
    }];
    
    NSArray *buttons = @[@"开始动画"];
    for (NSInteger i = 0; i < buttons.count; ++i) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.view addSubview:button];
        [button setTitle:buttons[i] forState:UIControlStateNormal];
        [button setTitleColor:UIColorFromRGB(0x333333) forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = i + 1;
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self.view.mas_centerX).mas_offset(0);
            make.top.mas_equalTo(animationView.mas_bottom).mas_offset(20 + (i * 40 + 20));
            make.width.mas_equalTo(100);
            make.height.mas_equalTo(40);
        }];
    }
    
    
    self.str1 = @"夏小汐一个平凡到不能在平凡的女孩，一直相信着长大就会遇见属于自己的王子。但在樱花高中遇见了陌亦夜和伊辰这两个自己无法触及的男神以后，她的烦恼就没有停止过。明明知道就算知道了喜欢谁又能怎么样!但为了让自己的青春不留下遗憾，夏小汐鼓起勇气向自己喜欢的男神告白，开始了自己的初恋。但好景不长，到后来夏小汐才知道所有的一切都是自己的一厢情愿，分手后的夏小汐开始变得坚强；当她华丽的转身归来之时，曾经的那个男神居然说要回到自己的身边，但那时候的夏小汐身边已经出现了一个一直陪伴他的韩国当红明星安亦北，夏小汐究竟会如何选择？是选择曾经的爱;还是现在没有发现的爱......但夏小汐注定是一个幸运的女孩！";
    
    self.titleLableBig = animationView;
}

-(void)buttonAction:(UIButton *)sender
{
    switch (sender.tag) {
        case 1:
        {
            NSLog(@"开始动画");
            NSThread *thred = [[NSThread alloc]initWithTarget:self selector:@selector(animationForShowtitleLableBigLabelText) object:nil];
            self.titleLableBig.text = @"";
            [thred start];
            sender.enabled = NO;
            [sender setTitleColor:UIColorFromRGB(0x999999) forState:UIControlStateNormal];
        }
            break;
        case 2:
        {
            NSLog(@"结束动画");
//            [_thred cancel];
        }
            break;
            
        default:
            break;
    }
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0 , 100, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    
    
   
}

- (void)animationForShowtitleLableBigLabelText
{
    //这是子线程里处理动画的方式 str1是我要处理的文本 , self.titleLableBig 是我用于显示这个文本的label  下面是执行动画的循环 一共执行 [_str1 length] 是字符串的长度 每执行一次,在主线程之中刷新ui 子线程休眠0.1秒中用于显示 每次文本的不同 就显示出 上一次笔者一次 少一个文本的效果了
    for (NSInteger i = 0 ; i < [_str1 length] ;i++)
    {
        //回到主线程
        [self performSelectorOnMainThread:@selector(refreUIFortitleLableBigLabelText:) withObject:[_str1 substringWithRange:NSMakeRange(0,i+1)]waitUntilDone:YES];
        [NSThread sleepForTimeInterval:0.1f];
    }
    
    NSLog(@"动画结束");
    //回到主线程
    [self performSelectorOnMainThread:@selector(refreshUI) withObject:nil waitUntilDone:YES];
    
}

-(void)refreshUI
{
    UIButton *startAnimationView = [self.view viewWithTag:1];
    startAnimationView.enabled = YES;
    [startAnimationView setTitleColor:UIColorFromRGB(0x333333) forState:UIControlStateNormal];
}

- (void)refreUIFortitleLableBigLabelText:(NSString *)str
{
    self.titleLableBig.text = str;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
